package com.ruoyi.table.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.idempotent.annotation.RepeatSubmit;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.table.domain.TableArea;
import com.ruoyi.table.service.TableAreaService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 表格区域控制器
 *
 * @author Administrator
 * @date 2023/03/06
 */
@RestController
@RequestMapping("/area")
@RequiredArgsConstructor
public class TableAreaController extends BaseController {

    private final TableAreaService tableAreaService;

    /**
     * 列表
     *
     * @param tableArea 表区域
     * @param pageQuery 页面查询
     * @return {@link TableDataInfo}<{@link TableArea}>
     */
    @GetMapping("/page")
    @SaCheckPermission("table:area:select")
    public TableDataInfo<TableArea> pageTableArea(TableArea tableArea, PageQuery pageQuery) {
        return tableAreaService.pageTableArea(tableArea, pageQuery);
    }

    /**
     * 列表
     *
     * @return {@link R}<{@link List}<{@link TableArea}>>
     */
    @GetMapping
    @SaCheckPermission("table:area:select")
    public R<List<TableArea>> listTableArea() {
        return R.ok(tableAreaService.list());
    }

    /**
     * 更新状态
     *
     * @param tableArea 表区域
     * @return {@link R}<{@link Void}>
     */
    @PutMapping("/status")
    @SaCheckPermission("table:area:update")
    public R<Void> updateStatus(@RequestBody TableArea tableArea) {
        return tableAreaService.updateStatus(tableArea) ? R.ok() : R.fail("部分桌台使用中,无法停用区域");
    }


    /**
     * 更新
     *
     * @param tableArea 表区域
     * @return {@link R}<{@link Void}>
     */
    @PutMapping
    @RepeatSubmit
    @SaCheckPermission("table:area:update")
    public R<Void> update(@RequestBody TableArea tableArea) {
        return toAjax(tableAreaService.updateById(tableArea));
    }

    /**
     * 保存
     *
     * @param tableArea 表区域
     * @return {@link R}<{@link Void}>
     */
    @RepeatSubmit
    @PostMapping
    @SaCheckPermission("table:area:save")
    public R<Void> save(@RequestBody TableArea tableArea) {
        return toAjax(tableAreaService.saveTableArea(tableArea));
    }

    /**
     * 得到桌台区域
     *
     * @param areaId 区域id
     * @return {@link R}<{@link TableArea}>
     */
    @GetMapping("/{areaId}")
    @SaCheckPermission("table:area:select")
    public R<TableArea> getTableArea(@PathVariable("areaId") Long areaId) {
        return R.ok(tableAreaService.getById(areaId));
    }

    /**
     * 删除
     *
     * @param areaId 区域id
     * @return {@link R}<{@link Void}>
     */
    @DeleteMapping
    @SaCheckPermission("table:area:delete")
    public R<Void> delete(@RequestBody Long areaId) {
        return tableAreaService.deleteTableAreaById(areaId) ? R.ok() : R.fail("此区域下分配了桌台,不能删除");
    }

}
